/*
    Authour: Christopher Diggins
    License: MIT Licence 1.0
    Website: http://www.cdiggins.com
*/

#ifndef CVML_ARRAY_HPP
#define CVML_ARRAY_HPP

template<typename size_T, typename T>
struct Array : Serializable
{
	size_T cnt;
	T* data;

	Array()
		: cnt(0), data(NULL)
	{ }

	~Array()
	{
		Clear();
	}
	
	void Reallocate(size_t n)
	{
		Clear();
		cnt = n;
		data = new T[cnt];
	}

	const T& operator[](size_t n) const {
		return data[n];
	}

	T& operator[](size_t n) {
		return data[n];
	}

	void Serialize(Stream& stream)
	{
		if (stream.IsReading()) 
		{
			stream.Read(cnt);
			Reallocate(cnt);
		}
		else 
		{
			stream.Write(cnt);
		}
		for (size_t i=0; i < cnt; ++i)
			stream.Serialize(data[i]);
	}

	void Clear()
	{
		if (data == NULL)
			return;
		delete[] data;
		cnt = 0;
		data = NULL;
	}

	size_T Count() 
	{
		return cnt;
	}

	size_T GetTotalSize() 
	{
		return sizeof(size_T) + sizeof(T) * cnt;
	}

	T* Begin() 
	{
		return data;
	}

	T* End()
	{
		return data + cnt;
	}
};

#endif

